package edu.uta.cfl.engine;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;

import edu.uta.cfl.data.Combo;
import edu.uta.cfl.data.Component;
import edu.uta.cfl.data.Parameter;

public class ComponentManager {

	public static int compFrequencyTCLst(Component cmp,
			ArrayList<int[]> tcList, ArrayList<Parameter> prms) {
		int fr = 0;
		for (int[] tc : tcList) {
			if (tc[prms.indexOf(cmp.getParameter())] == cmp.getValue())
				fr++;
		}
		return fr;
	}

	public static int compFrequencyCmb(Component cmp, ArrayList<Combo> cmbList) {
		int fr = 0;
		for (Combo cmb : cmbList) {
			if (isComboHas(cmp, cmb))
				fr++;
		}
		return fr;
	}

	public static boolean isComboHas(Component cmp, Combo cmb) {
		for (Component cmpInCmb : cmb.getComponent())
			if (cmpInCmb.getParameter().equals(cmp.getParameter())
					&& cmpInCmb.getValue() == cmp.getValue())
				return true;
		return false;

	}

	public static int idxCmpLst(Component cmp, ArrayList<Component> cmpLst) {
		for (Component cmpInLst : cmpLst) {
			if (cmp.getParameter().equals(cmpInLst.getParameter())
					&& cmp.getValue() == cmpInLst.getValue())
				return cmpLst.indexOf(cmpInLst);

		}

		return -1;
	}

	public static int getNMinRankVal(Parameter prm, ArrayList<Component> cmps,
			int n) {
		ArrayList<Double> rank = new ArrayList<Double>();
		// ArrayList<Double> distinctRank = new ArrayList<Double>();

		for (int i = 0; i < prm.getValues().size(); i++) {
			boolean match = false;
			for (Component cmp : cmps)
				if (cmp.getParameter() == prm && cmp.getValue() == i && !match) {
					match = true;
					rank.add(cmp.getRankValue());
					/*
					 * if ( !distinctRank.contains(cmp.getRankValue())){
					 * distinctRank.add(cmp.getRankValue()); numRank.add(1); }
					 * else { int idx =
					 * distinctRank.indexOf(cmp.getRankValue());
					 * numRank.set(idx, numRank.get(idx)+1); }
					 */
				}
			if (!match) {
				rank.add((double) 0);
				/*
				 * if ( !distinctRank.contains((double) 0)){
				 * distinctRank.add((double)0); numRank.add(1); } else { int idx
				 * = distinctRank.indexOf(0); numRank.set(idx,
				 * numRank.get(idx)+1); }
				 */
			}
		}

		ArrayList<Double> sortedRank = new ArrayList<Double>();
		for (int i = 0; i < rank.size(); i++) {
			sortedRank.add(rank.get(i));
		}
		Collections.sort(sortedRank);

		if (n > rank.size())
			return -1;

		double rankVal = sortedRank.get(n - 1);
		int tmp = 0;

		for (int tmpIdx = n - 2; tmpIdx >= 0; tmpIdx--)
			if (sortedRank.get(tmpIdx) == rankVal) {
				tmp++;
			}

		for (int i = 0; i < rank.size(); i++) {
			if (rank.get(i) == rankVal) {
				if (tmp == 0)
					return i;
				tmp--;
			}
		}

		return -1;

		/*
		 * numRank.add(i); double val= rank.get(i); int minIdx =i; for(int
		 * j=0;j<rank.size();j++){ if ( rank.get(minIdx) >= rank.get(j)&&
		 * !numRank.contains(j) ){ minIdx = j; } } // double min=
		 * rank.get(minIdx); rank.set(minIdx,2.0); // rank.set(i,min);
		 * numRank.add(minIdx);
		 * 
		 * }
		 * 
		 * 
		 * if ( n > numRank.get(numRank.size()-1)) return -1; else for (int i=0
		 * ; i< rank.size();i++){ if (numRank.get(i)==n)
		 * 
		 * return ;
		 */

	}

	public static ArrayList<Component> mergeRankValue(
			ArrayList<Component> first, ArrayList<Component> second,
			ArrayList<Parameter> prms) {
		ArrayList<Component> output = new ArrayList<Component>();

		ArrayList<Component> fRepDel = new ArrayList<Component>();
		ArrayList<Component> sRepDel = new ArrayList<Component>();

		for (Component cmp : first)
			if (idxCmpLst(cmp, fRepDel) == -1)
				fRepDel.add(cmp);

		for (Component cmp : second)
			if (idxCmpLst(cmp, sRepDel) == -1)
				sRepDel.add(cmp);

		output.addAll(fRepDel);
		int idx = -1;
		for (Component cmp : sRepDel) {
			if ((idx = idxCmpLst(cmp, output)) != -1) {
				Component newCmp = new Component(cmp.getParameter(),
						cmp.getValue());
				newCmp.setRankValue((cmp.getRankValue() + output.get(idx)
						.getRankValue()) / 2);
				output.add(newCmp);
				output.remove(idx);
			} else {
				output.add(cmp);
			}

		}
		return output;
	}

}
